#ifdef WITH_OPENGL
#include "context_opengl.h"

using namespace krryn::system_imp;

context_opengl::context_opengl(){
	// ?
	enum{l_Size = 100 };
	m_TexturesEnabled = new bool[l_Size];
	for(int i = 0; i < l_Size; i++)
		m_TexturesEnabled[i] = false;
}

context_opengl::~context_opengl(){
	delete m_TexturesEnabled;
}

void context_opengl::enable_texture(size_t a_Index){
	// we don't know if the texture is active so we activate it anyway
	glActiveTexture(GL_TEXTURE0 + a_Index);

	if(!m_TexturesEnabled[a_Index]){
		m_TexturesEnabled[a_Index] = true;
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	}
}

void context_opengl::disable_texture(unsigned int a_Index){
	if(m_TexturesEnabled[a_Index]){
		glActiveTexture(GL_TEXTURE0 + a_Index);
		m_TexturesEnabled[a_Index] = false;
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	}
}

void context_opengl::enable(GLuint a_State){
	std::map<GLuint, bool>::iterator i = m_States.find(a_State);

	// not found or disabled so enable the state
	if(i == m_States.end() || i->second == false){
		glEnableClientState(a_State);
		m_States[a_State] = true;
	}
}

void context_opengl::disable(GLuint a_State){
	std::map<GLuint, bool>::iterator i = m_States.find(a_State);

	// disable state only if it has been enabled before
	if(i != m_States.end() && i->second == true){
		glDisableClientState(a_State);
		m_States[a_State] = false;
	}
}

#endif